package com.eureka.eurekaserviceinvoker.rule;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.Objects;

/**
 * 实现IRule接口，自定义负载规则
 * Created by earl on 2018/4/25.
 */
public class MyRule implements IRule{

    ILoadBalancer lb;
    public MyRule(){}

    public MyRule(ILoadBalancer lb){
        this.lb = lb;
    }

    @Override
    public Server choose(Object key){
        System.out.println("key  =  " +key);
        //获取所有服务器
        List<Server> serverList = lb.getAllServers();
        //设置用于返回第一个服务器
        System.out.println("设置永远返回第一个服务器 :" +serverList.get(0).getHostPort());
        return  serverList.get(0);
    }

    @Override
    public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
        this.lb = iLoadBalancer;
    }

    @Override
    public ILoadBalancer getLoadBalancer() {
        return lb;
    }

}
